package cn.itcast.bos.action.base;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.ws.rs.core.MediaType;

import org.apache.commons.lang.StringUtils;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;

import cn.itcast.bos.domain.base.Courier;
import cn.itcast.bos.domain.base.FixedArea;
import cn.itcast.bos.domain.base.TakeTime;
import cn.itcast.bos.service.base.CourierService;
import cn.itcast.bos.service.base.FixedAreaService;
import cn.itcast.bos.service.base.TakeTimeService;
import cn.itcast.crm.domain.Customer;
import freemarker.core.CustomAttribute;

@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("prototype")
public class FixedAreaAction extends BaseAction<FixedArea> {

	@Autowired
	private FixedAreaService fixedAreaService;

	@Action(value = "addFixedArea", results = { @Result(name = "success", type = "redirect", location = "./pages/base/fixed_area.html") })
	public String saveFixedArea() {

		fixedAreaService.save(model);

		return SUCCESS;
	}

	@Action(value = "searchFixedArea", results = { @Result(name = "success", type = "json") })
	public String searchFixedArea() {

		Pageable pageable = new PageRequest(page - 1, rows);
		System.err.println(model.getCompany());
		Specification<FixedArea> specification = new Specification<FixedArea>() {

			@Override
			public Predicate toPredicate(Root<FixedArea> root,
					CriteriaQuery<?> query, CriteriaBuilder cb) {

				List<Predicate> list = new ArrayList<Predicate>();

				if (StringUtils.isNotBlank(model.getId())) {
					Predicate p1 = cb.equal(root.get("id").as(String.class),
							model.getId());
					list.add(p1);
				}

				if (StringUtils.isNotBlank(model.getCompany())) {
					Predicate p2 = cb.like(
							root.get("company").as(String.class),
							"%" + model.getCompany() + "%");
					list.add(p2);
				}
				return cb.and(list.toArray(new Predicate[0]));
			}
		};
		Page<FixedArea> page = fixedAreaService.searchFixedArea(specification,
				pageable);
		pushPageDataToValueStack(page);
		return SUCCESS;
	}

	@Action(value="noassociationscustomer",results={@Result(name="success",type="json")})
	public String getNoAssociationsCustomer(){
		Collection<? extends Customer> collection = WebClient.create("http://localhost:9999/crm_management/services/customerService/noassociationcustomers").accept(MediaType.APPLICATION_JSON).getCollection(Customer.class);
		ActionContext.getContext().getValueStack().push(collection);
		return SUCCESS;
	}
	
	
	
	@Action(value="associationscustomer",results={@Result(name="success",type="json")})
	public String getAssociationsCustomer(){
		Collection<? extends Customer> collection = WebClient.create("http://localhost:9999/crm_management/services/customerService/associationfixedareacustomers/"+model.getId())
				.accept(MediaType.APPLICATION_JSON).type(MediaType.APPLICATION_JSON).getCollection(Customer.class);
		ActionContext.getContext().getValueStack().push(collection);
		return SUCCESS;
	}
	
	private String[] customerIds=null;
	
	public void setCustomerIds(String[] customerIds) {
		this.customerIds = customerIds;
	}

	@Action(value="setcustomertoassociation",results={@Result(name="success",type="redirect",location="./pages/base/fixed_area.html")})
	public String setCustomerToAssociation(){
		
		String string = StringUtils.join(customerIds, ",");
		if (string==null) {
			string="";
		}
		
		WebClient.create("http://localhost:9999/crm_management/services/customerService/associationcustomerstofixedarea?noAssociationCustomerId="+string+"&fixedAreaId="+model.getId()).put(null);
		
		return SUCCESS;
	}
	
	private Integer courierId;
	private Integer takeTimeId;
	
	public void setCourierId(Integer courierId) {
		this.courierId = courierId;
	}

	public void setTakeTimeId(Integer takeTimeId) {
		this.takeTimeId = takeTimeId;
	}

	@Action(value="associationCourierForFixedArea",results={@Result(name="success",type="redirect",location="./pages/base/fixed_area.html")})
	public String associationCourier(){

		fixedAreaService.associationCourier(model,courierId,takeTimeId);
		return SUCCESS;
	}
}
